package com.qiaofang.applet.service.member.service.impl;

import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.common.util.RedisUtil;
import com.qiaofang.applet.constants.BusinessConstants;
import com.qiaofang.applet.constants.MemberOperateTypeConstants;
import com.qiaofang.applet.service.member.domain.Member;
import com.qiaofang.applet.service.member.domain.MemberOperate;
import com.qiaofang.applet.service.member.repository.MemberOperateRepository;
import com.qiaofang.applet.service.member.repository.MemberRepository;
import com.qiaofang.applet.service.member.service.MemberExchangeService;
import com.qiaofang.applet.service.member.service.dto.MemberExchangeAddDto;
import com.qiaofang.applet.service.member.service.mapstruct.MemberOperateMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author Zym
 */
@Slf4j
@Service
public class MemberExchangeServiceImpl implements MemberExchangeService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MemberOperateRepository memberOperateRepository;

    @Autowired
    private MemberOperateMapper memberOperateMapper;

    @Autowired
    private MemberRepository memberRepository;

    /**
     * 添加一条交流记录（聊天、打电话）
     * @param memberExchangeAddDto
     * @return
     */
    @Override
    public void addExchangeRecord(MemberExchangeAddDto memberExchangeAddDto) {
        //过滤自己跟自己发
        String chatAndCallKey = getChatAndCallKey(memberExchangeAddDto.getEmployeeUniqueUuid(), memberExchangeAddDto.getMemberId());
        Object objVaule = redisUtil.get(chatAndCallKey);
        if (objVaule != null) {
            return;
        }
        Member member = memberRepository.findByCorporationCodeAndId(QfContextHolder.getContext().getCorporationCode(), memberExchangeAddDto.getMemberId());
        if (member != null && StringUtils.equals(memberExchangeAddDto.getEmployeeUniqueUuid(), member.getEmployeeUniqueUuid())) {
            redisUtil.set(chatAndCallKey, 1);
            return;
        }

        //同一个聊天对话 15分钟才算一次
        //String chatKey = RedisKeyUtil.getEmployeeMemberChatKey(memberExchangeAddDto.getEmployeeUniqueUuid(), memberExchangeAddDto.getMemberId());
        String chatKey = null;
        if (MemberOperateTypeConstants.CHAT.getValue().equals(memberExchangeAddDto.getOperateType())) {
            chatKey = getRedisKey(memberExchangeAddDto.getEmployeeUniqueUuid(), memberExchangeAddDto.getMemberId());
            Object obj = redisUtil.get(chatKey);
            if (obj != null) {
                return;
            }
        }
        MemberOperate memberOperate = memberOperateMapper.exchangeDtoToDomain(memberExchangeAddDto);
        memberOperate.setBusinessUuid(memberOperate.getEmployeeUniqueUuid());
        memberOperate.setBusinessType(BusinessConstants.EMPLOYEE.getCode());
        memberOperate.setCreateTime(new Date());
        memberOperate.setCorporationCode(QfContextHolder.getContext().getCorporationCode());
        try {
            memberOperateRepository.insert(memberOperate);
            if (MemberOperateTypeConstants.CHAT.getValue().equals(memberExchangeAddDto.getOperateType())) {
                redisUtil.set(chatKey, 1, 900000L, TimeUnit.MILLISECONDS);
            }
        } catch (Exception e) {
            log.error("新增互动记录失败:{}", e);
            throw new BusinessException("新增互动记录失败");
        }
    }

    private String getRedisKey(String employeeUniqueUuid, String memberId){
        return "applet_" + "employeeMemberChatKey_" + employeeUniqueUuid +"_"+ memberId;
    }

    private String getChatAndCallKey(String employeeUniqueUuid, String memberId){
        return "applet_" + "chatAndCallKey_" + employeeUniqueUuid +"_"+ memberId;
    }

}
